using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Terra.Data.Repository;
using Terra.Data.Entities.Identity;
using System.Linq.Expressions;
using Terra.Data.Models.Identity;

namespace Terra.Data.Repository.Identity;

/// <summary>
/// 角色仓储接口
/// </summary>
public interface IRoleRepository : IRepository<Role, Guid>
{
    /// <summary>
    /// 根据角色名查找角色
    /// </summary>
    /// <param name="roleName">角色名</param>
    /// <returns>角色实体</returns>
    Task<Role> FindByNameAsync(string roleName);

    /// <summary>
    /// 获取角色的用户列表
    /// </summary>
    /// <param name="roleId">角色ID</param>
    /// <returns>用户列表</returns>
    Task<IList<User>> GetUsersInRoleAsync(Guid roleId);

    /// <summary>
    /// 获取系统角色列表
    /// </summary>
    /// <returns>系统角色列表</returns>
    Task<IList<Role>> GetSystemRolesAsync();

    /// <summary>
    /// 分页查询角色列表
    /// </summary>
    /// <param name="query">查询条件</param>
    /// <returns>角色列表和总数</returns>
    Task<(List<Role> Items, int TotalCount)> GetPagedRolesAsync(RoleQueryDto query);

    /// <summary>
    /// 检查角色名是否已存在
    /// </summary>
    /// <param name="roleName">角色名</param>
    /// <param name="excludeRoleId">排除的角色ID</param>
    /// <returns>是否存在</returns>
    Task<bool> ExistsByNameAsync(string roleName, Guid? excludeRoleId = null);

    /// <summary>
    /// 批量删除角色
    /// </summary>
    /// <param name="roleIds">角色ID列表</param>
    /// <returns>删除结果和失败ID列表</returns>
    Task<(bool Succeeded, List<Guid> FailedIds)> DeleteRolesAsync(IEnumerable<Guid> roleIds);

    /// <summary>
    /// 为角色指定用户
    /// </summary>
    /// <param name="roleId">角色ID</param>
    /// <param name="userIds">用户ID列表</param>
    /// <returns>是否成功</returns>
    Task<bool> AssignUsersToRoleAsync(Guid roleId, IEnumerable<Guid> userIds);

    /// <summary>
    /// 从角色中移除用户
    /// </summary>
    /// <param name="roleId">角色ID</param>
    /// <param name="userIds">用户ID列表</param>
    /// <returns>是否成功</returns>
    Task<bool> RemoveUsersFromRoleAsync(Guid roleId, IEnumerable<Guid> userIds);

    /// <summary>
    /// 获取角色选择列表
    /// </summary>
    /// <returns>角色选择列表</returns>
    Task<List<RoleSelectDto>> GetRoleSelectListAsync();

    /// <summary>
    /// 获取角色的权限ID列表
    /// </summary>
    Task<List<Guid>> GetRolePermissionIdsAsync(Guid roleId);

    /// <summary>
    /// 保存角色权限关联
    /// </summary>
    Task SaveRolePermissionsAsync(Guid roleId, List<Guid> permissionIds);

    /// <summary>
    /// 删除角色的所有权限关联
    /// </summary>
    Task DeleteRolePermissionsAsync(Guid roleId);
}